package org.yangspring;

/**
 * Created by whyang on 2017/03/02.
 */

import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.*;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextCleanupListener;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.*;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.mvc.ServletForwardingController;
import org.springframework.web.servlet.mvc.ServletWrappingController;
import org.springframework.web.servlet.mvc.UrlFilenameViewController;
import org.springframework.web.util.IntrospectorCleanupListener;
import org.springframework.web.util.WebAppRootListener;

import javax.servlet.*;
import javax.servlet.annotation.HandlesTypes;
import javax.servlet.http.HttpServlet;
import java.util.EventListener;


/**********
 * 声明一个事件 onStartup；
 * 这个事件可能与Servlet有关系，由Tomcat/Jetty启动。
 *
 * 可以通过注解@HandlesTypes方式，
 * 也可以通过实现接口WebApplicationInitializer
 * *********/
@HandlesTypes(WebApplicationInitializer.class)
public class FrankieWebAppInitializer implements WebApplicationInitializer {

    private static final Log logger = LogFactory.getLog(FrankieWebAppInitializer.class);

//    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext webApplicationContext =
                new AnnotationConfigWebApplicationContext();
        webApplicationContext.register(AppConfig.class);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
        ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatc89898989herServlet", dispatcherServlet);
//        dynamic.addMapping("/");

        /*logger.warn( "111111111111AAAA:\t" + "\ngetDisplayName" +
                webApplicationContext.getDisplayName() + "\ngetId" +
                webApplicationContext.getId() + "\ngetApplicationName" +
                webApplicationContext.getApplicationName() + "\ngetBeanFactory" +
                webApplicationContext.getBeanFactory() + "\ngetNamespace" +
                webApplicationContext.getNamespace() + "\ngetBeanDefinitionCount" +
                webApplicationContext.getBeanDefinitionCount() + "\ngetBeanDefinitionNames" +
                webApplicationContext.getBeanDefinitionNames() + "\ngetEnvironment" +
                webApplicationContext.getEnvironment() + "\ngetParent" +
                webApplicationContext.getParent() + "\ngetParentBeanFactory" +
                webApplicationContext.getParentBeanFactory() + "\ngetServletConfig" +
                webApplicationContext.getServletConfig() + "\ngetServletContext" +
                webApplicationContext.getServletContext() + "\ngetConfigLocations" +
                webApplicationContext.getConfigLocations() + "\ngetClassLoader" +
                webApplicationContext.getClassLoader()

        );*/

        /*logger.warn( "222222222222AAAA:\t" + "\ngetServletConfig" +
                dispatcherServlet.getServletConfig() + "\ngetNamespace" +
                dispatcherServlet.getNamespace() + "\ngetEnvironment" +
                dispatcherServlet.getEnvironment() + "\ngetServletContext" +
                dispatcherServlet.getServletContext() + "\ngetContextAttribute" +
                dispatcherServlet.getContextAttribute() + "\ngetContextConfigLocation" +
                dispatcherServlet.getContextConfigLocation() + "\ngetContextId" +
                dispatcherServlet.getContextId() + "\ngetServletInfo" +
                dispatcherServlet.getServletInfo() + "\ngetServletName" +
                dispatcherServlet.getServletName() + "\ngetMultipartResolver" +
                dispatcherServlet.getMultipartResolver() + "\ngetServletConfig" +
                dispatcherServlet.getThemeSource() + "\ngetInitParameterNames" +
                dispatcherServlet.getInitParameterNames() + "\ngetWebApplicationContext" +
                dispatcherServlet.getWebApplicationContext() + "\ngetClass" +
                dispatcherServlet.getClass()
        );*/


        logger.warn( "333333333333AAAA:\t" + "\ngetClass:\t" +
                dynamic.getClass() + "\ngetClassName:\t" +
                dynamic.getClassName() + "\ngetName:\t" +
                dynamic.getName() + "\ngetRunAsRole:\t" +
                dynamic.getRunAsRole() + "\ngetInitParameters:\t" +
                dynamic.getInitParameters() + "\ngetMappings:\t" +
                dynamic.getMappings()
        );
    }


    public void test(){

        InitializingBean initBean = null;

        BeanFactory beanFac = null;

        DefaultListableBeanFactory defLstBeanFac = null;

        BeanFactoryPostProcessor beanFacPostPrcs;

        BeanPostProcessor beanPostPrcs;
        InstantiationAwareBeanPostProcessor instAwrBeanPostPrcs;
        SmartInstantiationAwareBeanPostProcessor xxx;
        InstantiationAwareBeanPostProcessorAdapter instAwrBeanPostPrcsAdpt;

        BeanFactoryAware beanFacAwr;

        AbstractApplicationContext absAppCntx;
        AbstractRefreshableWebApplicationContext absRefWebAppCntx;
        ConfigurableApplicationContext cfgAppCntx;  //  Interface
        ConfigurableWebApplicationContext cfgWebAppCntx;    //  Interface

        ConfigurableApplicationContext confgAC;


        SpringFactoriesLoader springFacLdr;

//        SpringApplicationRunListener sss;

        {

            EventListener evntLsnr;
            {
                ServletContextListener svltCntxLsnr;
                {
                    ContextLoaderListener cntxLdrLsnr;
                    ContextCleanupListener cntxClnLsnr;
                    FileCleanerCleanup fileClnLsnr;
                    IntrospectorCleanupListener introClnLsnr;
                    WebAppRootListener webAppRootLsnr;
                }
            }

            ContextLoader cntxLdr;

            GenericServlet genericServlet;
            {
                HttpServlet httpServlet;
                {
                    HttpServletBean httpServletBean;
                    {
                        FrameworkServlet frameworkServlet;
                        {
                            DispatcherServlet dispatcherServlet;
                        }
                    }
                }
            }

        }

    }

    public void test2(){

        ServletForwardingController svltFwdCtlr;
        ServletWrappingController svltWrppCtlr;

        UrlFilenameViewController urlFlViewCrlr;
        ParameterizableViewController paraViewCtlr;

        View v;

        ViewResolver vr;

    }

}


